﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.EmailsAndWorkflows
{
    public partial class ApplicatinWorkflows : BLL.WIN.Templates.DevxConfigurationUserControlTemplate
    {
        DAL.Database.APPLICATION_WORKFLOW CURRENT_SELECTED_APPLICATION_WORKFLOW
        { get; set; }

        public ApplicatinWorkflows()
        {
            InitializeComponent();

            btnAddNew.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnAddNew_ItemClick);
            btnSaveUpdate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnSaveUpdate_ItemClick);
            btnDeleteItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnDeleteItem_ItemClick);
            btnRefresh.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnRefresh_ItemClick);

            RELOAD_ALL_APPLICATION_WORKFLOW();
        }

        private void btnSaveUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dxErrorProvider1.ClearErrors();
            bool has_workflow_name = !string.IsNullOrEmpty(txtWorkflowName.Text);
            if (has_workflow_name)
            {

                decimal? aPPLICATION_WORFLOW_ID = 0;
                if (CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORFLOW_ID > 0) //update
                {
                    aPPLICATION_WORFLOW_ID = CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORFLOW_ID;

                    PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOWSUpdate
                        (
                            aPPLICATION_WORFLOW_ID.Value,
                            txtWorkflowName.Text,
                            txtNotes.Text,
                            !chkIsValid.Checked
                        );

                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcWorkflowName, txtWorkflowName.Text);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcNotes, txtNotes.Text);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcIsValid, !chkIsValid.Checked);

                }
                else
                {
                    PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOWSInsert
                        (
                            ref aPPLICATION_WORFLOW_ID,
                             txtWorkflowName.Text,
                            txtNotes.Text,
                            !chkIsValid.Checked
                        );

                    RELOAD_ALL_APPLICATION_WORKFLOW();
                }

                CURRENT_SELECTED_APPLICATION_WORKFLOW = (from workflow in PRO_DOCSDatabaseDataContext.APPLICATION_WORKFLOWs
                                                         where workflow.APPLICATION_WORFLOW_ID.Equals(aPPLICATION_WORFLOW_ID.Value)
                                                         select workflow).FirstOrDefault();


                SAVE_APPLICATION_WORKFLOW_DOCUMENT_TYPES(aPPLICATION_WORFLOW_ID.Value);

            }
            else
            {
                dxErrorProvider1.SetError(txtWorkflowName, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "workflow name"));
            }
        }


        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            CLEAR_WORKFLOW_TABS();

            if (e.FocusedRowHandle > -1)
            {
                CURRENT_SELECTED_APPLICATION_WORKFLOW = gridView1.GetFocusedRow() as DAL.Database.APPLICATION_WORKFLOW;
                CURRENT_SELECTED_APPLICATION_WORKFLOW = (from workflow in PRO_DOCSDatabaseDataContext.APPLICATION_WORKFLOWs
                                                         where workflow.APPLICATION_WORFLOW_ID.Equals(CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORFLOW_ID)
                                                         select workflow).FirstOrDefault();
                LOAD_CURRENT_SELECTED_APPLICATION_WORKFLOW();
            }
        }

        private void LOAD_CURRENT_SELECTED_APPLICATION_WORKFLOW()
        {
            txtWorkflowName.Text = CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORKFLOW_NAME;

            chkIsValid.Checked = !CURRENT_SELECTED_APPLICATION_WORKFLOW.IS_DELETED.Value;

            txtNotes.Text = CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORKFLOW_NOTES;

            workflowDocumentTypes1.LOAD_WORKFLOW_DOCUMENT_TYPES(CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORFLOW_ID);

            workflowDocumentTypes1.Enabled = true;
        }

        private void SAVE_APPLICATION_WORKFLOW_DOCUMENT_TYPES(decimal aPPLICATION_WORFLOW_ID)
        {
            workflowDocumentTypes1.SAVE_WORKFLOW_DOCUMENT_TYPES(aPPLICATION_WORFLOW_ID);
        }

        private void btnDeleteItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORFLOW_ID > 0)
            {
                try
                {
                    PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOWSDelete(CURRENT_SELECTED_APPLICATION_WORKFLOW.APPLICATION_WORFLOW_ID);

                    gridView1.DeleteSelectedRows();
                }
                catch
                {
                    XtraMessageBox.Show("The current application workflow cannot be deleted as it is already used in another transaction(s)", "Action denied", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnAddNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CLEAR_WORKFLOW_TABS();
        }

        private void CLEAR_WORKFLOW_TABS()
        {
            CURRENT_SELECTED_APPLICATION_WORKFLOW = new DAL.Database.APPLICATION_WORKFLOW();
            txtWorkflowName.Text = string.Empty;
            chkIsValid.Checked = true;
            txtNotes.Text = string.Empty;
            workflowDocumentTypes1.CLEAR_SELECTED_DOCUMENT_TYPES();
            //workflowDocumentTypes1.Enabled = false;

        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RELOAD_ALL_APPLICATION_WORKFLOW();
        }

        private void RELOAD_ALL_APPLICATION_WORKFLOW()
        {
            APPLICATION_WORKFLOWS = PRO_DOCSDatabaseDataContext.APPLICATION_WORKFLOWs.ToList();
            gridControl1.DataSource = APPLICATION_WORKFLOWS;

            workflowDocumentTypes1.Enabled = APPLICATION_WORKFLOWS.Count > 0;
        }

        public List<DAL.Database.APPLICATION_WORKFLOW> APPLICATION_WORKFLOWS
        { get; set; }
    }
}
